#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
THN TELEPORT version 2

Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

This library is dual-licensed.
  + For commercial software, please obtain a commercial license from THN Solutions LLC.

  + For free software, this library is licensed under GPL version 3. A summary of GPLv3 is
    listed below. You should also find a copy of GPLv3 as file License_GPLv3.txt included
	with the source-code files.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 3 as published by
    the Free Software Foundation  of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#endregion

#region Using directives
using System;
using System.Windows.Forms;

using Thn;
using Thn.Application.Configuration;
using Thn.Communication.Teleporting;
using Thn.Communication.Teleporting.Logging;
using Thn.Communication.Teleporting.Settings;
using Thn.Communication.Teleporting.Connectors;
using Thn.Communication.Teleporting.Connectors.Remoting;

using Demo.Business;
#endregion

namespace Thn.Communication.Demo.Connectors.Remoting.Server
{
    public partial class fmMain : Form
    {
        #region Fields
        /// <summary>
        /// Server connector manager Start, Stop server
        /// </summary>
        ServerConnector mServer = null;
        string mGateway = "ProductService";
        #endregion

        #region Constructor
        public fmMain()
        {
            InitializeComponent();
        }
        #endregion

        #region Load
        private void fmMain_Load(object sender, EventArgs e)
        {
            if (!DesignMode)
            {
                ////auto start
                //btnStart_Click(this, EventArgs.Empty);

                ////enable remote log
                //RemotingLogger logger = new RemotingLogger();
                //logger.ConfigureForServer();
            }
        }
        #endregion

        #region Stopped
        void mServer_Stopped(object sender, EventArgs e)
        {
            if (!mServer.IsStarted) Log.Debug("Server has stopped");
            else throw new Exception("Server should be stopped at this point");
        }
        #endregion

        #region Started
        void mServer_Started(object sender, EventArgs e)
        {
            if (mServer.IsStarted)
            {
                //Log.Debug("Sever has started - Uri: {0} Port: {1} Formatter: {2} Channel: {3}", mServer.Uri, mServer.Port, mServer.Formatter, mServer.ChannelType);
                Log.Debug("Server has started");
                Log.Debug("      Uri: {0}", mServer.Uri);
                Log.Debug("      Port: {0}", mServer.Port);
                Log.Debug("      Channel: {0}", mServer.ChannelType);
                Log.Debug("      Formatter: {0}", mServer.Formatter);
            }
            else throw new Exception("Server should have started at this point");
        }
        #endregion

        #region Register Gateways
        bool mGatewaysRegistered = false;
        void RegisterGateways()
        {
            if (!mGatewaysRegistered)
            {
                Teleport.AddGateway(mGateway, new ProductService());

                //ensure that we only need to register once
                mGatewaysRegistered = true;
            }
        }
        #endregion

        #region Stop Server
        /// <summary>
        /// Stop Server. Stop connect
        /// </summary>
        private void btnStop_Click(object sender, EventArgs e)
        {
            mServer.Stop();
        }
        #endregion

        #region Start Server
        /// <summary>
        /// Server Start and listen connect from client
        /// </summary>
        private void btnStart_Click(object sender, EventArgs e)
        {
            //enable remote log
            RemotingLogger logger = new RemotingLogger();
            logger.ConfigureForServer();

            //register gateways if necessary
            RegisterGateways();

            TeleportConfigurator configurator = new TeleportConfigurator();
            mServer = configurator.Load() as ServerConnector;

            //Subscribe to server's events
            mServer.Started += new EventHandler(mServer_Started);
            mServer.Stopped += new EventHandler(mServer_Stopped);

            //Start Server
            mServer.Start();
        }
        #endregion
    }
}
